* Reset settings and initialize log file
launch, path("build/cps_bms_raking")

*-------------------------------------------------------------------------------
* Price and Wasserman (2024), "The Summer Drop in Female Employment"
*
* Description: Compute raked weights to reconcile stocks and flows.
*-------------------------------------------------------------------------------


* Load the main estimation sample
gzuse "$basepath/data/derived/cps_bms_sample.dta.gz", clear

* Retain select variables
keep pid tm wtfinl linked_monthly female emp unemp nlf

* Create a categorical variable summarizing labor force status in current month
gen byte lfs1 = (0 * emp) + (1 * unemp) + (2 * nlf)

* Record labor force status in the previous month
gen byte lfs0 = L.lfs1

* Tag linkable observations
gen byte linked = (linked_monthly == 1 & !missing(lfs0, lfs1))

* Discard variables we no longer need
drop linked_monthly emp unemp nlf

* Store the data
tempfile stash
save `stash'

* Skip months for which few/no observations are linkable to the previous month
bysort tm: egen share_linked = mean(linked)
tabstat share_linked if share_linked < .1, by(tm)

* Loop over months with linkable data
quietly levelsof tm if share_linked >= .1, local(tmlist)
foreach t of local tmlist {
	* Report progress
	display _n(2) "**************************************************"
	display       "* Progress: `=string(`t', "%tm")' ($S_DATE $S_TIME)"
	display       "**************************************************" _n

	* Loop over sex
	foreach f of numlist 0 1 {
		* Load sex-specific data from the previous and current months
		use if inlist(tm, `t' - 1, `t') & female == `f' using `stash', clear

		* Compute lagged/current population counts by labor force status
		gen _one = 1
		foreach k of numlist 0 1 {
			* k = 0: previous month; k = 1: current month
			if `k' == 0 local tk = `t' - 1
			if `k' == 1 local tk = `t'

			* Count all respondents (including those not linkable across months)
			quietly total _one if tm == `tk' [pw = wtfinl], over(lfs1)

			* Store the counts for use by ipfraking
			matrix lfs`k' = e(b)
			matrix rowname lfs`k' = lfs`k'
		}

		* Restrict to the current month
		quietly keep if tm == `t'

		* Compute raked weights for linked observations
		quietly ipfraking if linked == 1 [pw = wtfinl], ctotal(lfs0 lfs1) gen(wtraked) nograph

		* Save this month's data augmented with raked weights
		tempfile raked`t'_f`f'
		save `raked`t'_f`f''
	}
}

* Stack across months
clear
foreach t of local tmlist {
	foreach f of numlist 0 1 {
		append using `raked`t'_f`f''
	}
}

* Save the raked weights to disk
keep pid tm wtraked
order pid tm wtraked
gisid pid tm
sort pid tm
label variable wtraked "Raked longitudinal weights (reconciling employment stocks/flows)"
compress
save "$basepath/data/derived/cps_bms_raked_weights.dta", replace

* Merge these weights into the main CPS sample file
gzuse "$basepath/data/derived/cps_bms_sample.dta.gz", clear
capture confirm variable wtraked
if _rc != 0 {
	merge 1:1 pid tm using "$basepath/data/derived/cps_bms_raked_weights.dta", assert(1 3)
	assert inlist(tm, tm(1989m1), tm(1995m6), tm(1995m7), tm(1995m8), tm(1995m9)) if _merge == 1
	order wtraked, after(wtfinl)
	drop _merge
	gzsave, replace
}

* Close the log file
unlaunch
